package generate.generatecode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Scanner;

/**
 * @program: jdev-boot-core
 * @description
 * @author: JDev
 * @create: 2021-08-02 16:27
 **/
public class CheckBuildDirectory {

    private static final Logger logger = LoggerFactory.getLogger(CheckBuildDirectory.class);

    private CheckBuildDirectory(){

    }

    public static boolean checkExistFile(String buildPath) {
        File buildDirectory = new File(buildPath);
        if (buildDirectory.exists() && buildDirectory.isDirectory()){
            File[] files = buildDirectory.listFiles();
            if (files != null && files.length > 0) {
                logger.info("目录： {} 下已存在生成的文件。", buildPath);
                logger.info("再次执行代码生成功能的话，会覆盖现存文件，请输入（Y/N）。");
                Scanner scan = new Scanner(System.in);
                String inputCharacter = "";
                if (scan.hasNextLine()) {
                    inputCharacter = scan.next();
                }
                scan.close();
                if (!inputCharacter.equalsIgnoreCase("Y")) {
                    logger.info("输入了 {} 退出。", inputCharacter);
                    return false;
                }
                logger.info("输入了 {}  {} 下的文件。",inputCharacter,buildPath);
                return true;
            }
        }
        return true;
    }
}
